package com.ruoyi.kong.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kong.entity.*;
import com.ruoyi.kong.fegin.KongClientService;
import com.ruoyi.kong.service.IKongPluginInfoService;
import com.ruoyi.kong.utils.KongActiveProfileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;

@Service
public class KongPluginServiceImpl implements IKongPluginInfoService {

    @Autowired
    private KongActiveProfileUtil kongActiveProfileUtil;

    @Override
    public KongEntity<KongPlugin> plugins() {
        KongEntity<KongPlugin> plugins = KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).plugins();
        for (KongPlugin plugin : plugins.getData()){
            KongService kongService=null;
            KongRoute kongRoute =null;
            KongConsumer kongConsumer =null;
            if(!ObjectUtils.isEmpty(plugin.getService())){
                if(StringUtils.isNotEmpty(plugin.getService().getId())){
                    // 获取服务信息
                    String serviceId=plugin.getService().getId();
                    kongService= KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).getServiceById(serviceId);
                }
            }
            if(!ObjectUtils.isEmpty(plugin.getRoute())){
                if(StringUtils.isNotEmpty(plugin.getRoute().getId())){
                    String routeId= plugin.getRoute().getId();
                    kongRoute = KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).routeById(routeId);
                }
            }

            if(!ObjectUtils.isEmpty((plugin.getConsumer()))){
                if(StringUtils.isNotEmpty(plugin.getConsumer().getId())){
                    String consumerId=plugin.getConsumer().getId();
                    kongConsumer = KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).consumerById(consumerId);
                }
            }
            plugin.setApplyTo(kongService,kongRoute,kongConsumer);
        }

        return plugins;
    }
    @Override
    public KongPlugin pluginById(String id){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).pluginById(id);
    }

    @Override
    public KongEntity<KongPlugin> pluginsByRouteId(String routeId){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).pluginsByRouteId(routeId);
    }

    @Override
    public KongEntity<KongPlugin> pluginsByServiceId(String serviceId){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).pluginsByServiceId(serviceId);
    }

    public Map pluginSchemaByName(String name){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).pluginSchemaByName(name);
    }

    public KongEntity<KongPlugin> addPlugin(KongPlugin kongPlugin){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).addPlugin(kongPlugin);
    }

    public  void deletePlugin(String id){
        KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).deletePlugin(id);
    }

    public KongPlugin updatePlugin(String id, KongPluginVo plugin){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).updatePlugin(id,plugin);
    }
}
